package com.alstudio.net.protocol.xmpp.parser.iq;

import com.alstudio.net.protocol.xmpp.XmppListener;
import com.alstudio.net.protocol.xmpp.packact.IQ;
import com.alstudio.net.protocol.xmpp.parser.iq.IQParseEventHandler.IQXmlParseEventCallback;
import com.alstudio.utils.log.Mylog;

public class XmppPingPackactParser extends BaseIQParser implements
		IQXmlParseEventCallback {

	// 此条心跳包的类型。取值范围: "0" 服务器下发的S2C心跳包，程序必须响应。否则服务器将把连接断开
	// 。"1"服务器响应本程序上传的C2S心跳包。证明当前连接正常
	private int type = -1;
	private String id, from, to;

	@Override
	public void parseIQPackage(IQ packact, String stream, XmppListener listener)
			throws Exception {
		// TODO Auto-generated method stub
		mListener = listener;
		type = -1;
		id = packact.getId();
		from = packact.getFrom();
		to = packact.getTo();
		startParse(packact, stream, this);
		Mylog.d("开始解析");
	}

	@Override
	public void processErrorStartTag(String tag) {
		// TODO Auto-generated method stub

	}

	@Override
	public void processSetStartTag(String tag) {
		// TODO Auto-generated method stub

	}

	@Override
	public void processGetStartTag(String tag) {
		// TODO Auto-generated method stub
		if (tag.equals("ping")) {
			type = 0;
		}
	}

	@Override
	public void processResultStartTag(String tag) {
		// TODO Auto-generated method stub
		if (tag.equals("ping")) {
			type = 1;
		}
	}

	@Override
	public void processEndTag(String tag) {
		// TODO Auto-generated method stub

	}

	@Override
	public void processStartDocument() {
		// TODO Auto-generated method stub

	}

	@Override
	public void processEndDocument() {
		// TODO Auto-generated method stub
		onCallback();
	}

	@Override
	public void onCallback() {
		// TODO Auto-generated method stub
		if ((type != -1)) {
			if (mListener != null) {
				mListener.onJxaGetPingEvent(type, id, from, to);
			}
		}
	}

}
